﻿using System;
using System.Collections.Generic;
using System.Text;
using Library;
using System.Data.SqlClient;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 3/11/2011
-- Description:	Comment Class
-- =============================================
*/

namespace BusinessLayer
{
   public class Comment
    {
    #region "Variable and Properties"
    //Variable
        private int comment_ID;
        private int news_ID;
        private int member_ID;
        private string contents;
        private DateTime comment_Time;        
        private int comment_Status;

    //Properties
        public int Comment_ID
        {
            get { return comment_ID; }
            set { comment_ID = value; }
        }
        
        public int News_ID
        {
            get { return news_ID; }
            set { news_ID = value; }
        }
       
        public int Member_ID
        {
            get { return member_ID; }
            set { member_ID = value; }
        }
        
        public string Contents
        {
            get { return contents; }
            set { contents = value; }
        }

        public DateTime Comment_Time
        {
            get { return comment_Time; }
            set { comment_Time = value; }
        }

        public int Comment_Status
        {
            get { return comment_Status; }
            set { comment_Status = value; }
        }
    #endregion

    #region "Initial Methods"
        public Comment() { }

        public Comment(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                Comment_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    News_ID = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    Member_ID = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    Contents = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    Comment_Time = sdr.GetDateTime(4);
                if (!sdr.IsDBNull(5))
                    Comment_Status = sdr.GetInt32(5);
            }
        }
    #endregion

    #region "Business Methods"
    //Get All Comment
        public static List<Comment> GetAll(int Id)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@News_ID", Id);
            string cmd = "tbl_Comment_SHOWALL";
            List<Comment> lstComment = new List<Comment>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstComment.Add(new Comment(sdr));
            }

            return lstComment;
        }

    //Get One Comment
        public static Comment GetOne(int idComment)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Comment_SHOWONE";
            Comment comment = new Comment();

            dbh.AddParameter("@Comment_ID", idComment);
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                comment.Comment_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    comment.News_ID = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    comment.Member_ID = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    comment.Contents = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    comment.Comment_Time = sdr.GetDateTime(4);
                if (!sdr.IsDBNull(5))
                    comment.Comment_Status = sdr.GetInt32(5);
            }

            return comment;
        }

    //Insert Comment
        public static void Insert(Comment comment)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Comment_INSERT";

            dbh.AddParameter("@News_ID", comment.News_ID);
            dbh.AddParameter("@Member_ID", comment.Member_ID);
            dbh.AddParameter("@Contents", comment.Contents);
            dbh.AddParameter("@Comment_Time", comment.Comment_Time);
            dbh.AddParameter("@Comment_Status", comment.Comment_Status);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update Comment
        public static void Update(Comment comment)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Comment_UPDATE";

            dbh.AddParameter("@Comment_ID", comment.Comment_ID);
            dbh.AddParameter("@Contents", comment.Contents);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete Comment
        public static void Delete(int idComment)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Comment_DELETE";

            dbh.AddParameter("@Comment_ID", idComment);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    #endregion
    }
}
